package com.emokili.ewa.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.emokili.ewa.common.enums.ActiveEnum;
import com.emokili.ewa.core.frame.entity.*;
import com.emokili.ewa.core.frame.service.*;
import com.emokili.ewa.core.frame.vo.ProfileVo;
import com.emokili.ewa.core.frame.vo.Stats;
import com.emokili.ewa.portal.interceptor.LoginInterceptor;
import com.emokili.ewa.portal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private MemberService memberService;
    @Autowired
    MemberFrameService memberFrameService;
    @Autowired
    private PlayListService playListService;
    @Autowired
    private PlayListImageService playListImageService;
    @Autowired
    private MemberFriendService memberFriendService;

    @Override
    public ProfileVo getProfile() {
        MemberEntity memberEntity = getMemberEntity();
        Long id = memberEntity.getId();
        ProfileVo profileVo = new ProfileVo();
        MemberEntity byId = memberService.getById(id);
        profileVo.setFirstName(byId.getFirstName());
        profileVo.setLastName(byId.getLastName());
        profileVo.setLocale(byId.getLanguage());
        profileVo.setEmail(byId.getEmail());
        profileVo.setCurrencyCode("USD");
        profileVo.setCountry(byId.getCountry());
        profileVo.setLocale(byId.getLocale());

        List<MemberFrameEntity> frames = memberFrameService.list(new QueryWrapper<MemberFrameEntity>()
                .eq("member_id", byId.getId()));
        Stats stats = new Stats();
        stats.setFrames(frames.size());

        List<PlayListEntity> playListEntityList = playListService.list(new QueryWrapper<PlayListEntity>()
                .eq("member_id", byId.getId()));
        List<PlayListEntity> activePlayLists = playListEntityList.stream().filter(playList -> ActiveEnum.ACTIVE.getCode() == playList.getActive())
                .collect(Collectors.toList());
        int photos = 0;
        for (PlayListEntity playListEntity : activePlayLists){
            List<PlayListImageEntity> imageList = playListImageService.list(new QueryWrapper<PlayListImageEntity>().eq("play_list_id", playListEntity.getId()));
            photos += imageList.size();
        }
        stats.setPhotos(photos);
        List<MemberFriendEntity> memberFriendEntityList = memberFriendService.list(new QueryWrapper<MemberFriendEntity>().eq("member_id", byId.getId()));
        stats.setFriends(memberFriendEntityList.size());
        stats.setMaxFrames(5);
        profileVo.setStats(stats);
        return profileVo;
    }

    private MemberEntity getMemberEntity(){
        //todo 暂时写死
        //MemberEntity memberEntity = LoginInterceptor.loginUser.get();
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setId(1L);
        return memberEntity;
    }
}
